package com.imay.live.view.custom;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.CallSuper;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.imay.live.R;
import com.imay.live.analytics.MobclickAgent;
import com.imay.live.utils.ViewUtil;

/**
 * Created by jeaman on 2017/3/17.
 * 推流房间资料view
 */

public abstract class StartLiveView extends FrameLayout implements View.OnClickListener {
    private TextView mLocationTv;
    protected View mOtherView;
    protected Button mStartBtn;

    private OnActionClickListener mListener;

    public StartLiveView(Context context) {
        super(context);
        init(context);
    }

    public StartLiveView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public StartLiveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public StartLiveView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    @CallSuper
    protected void init(Context context) {
        setBackgroundColor(getResources().getColor(R.color.bg_7f00));
        inflate(context, getLayoutId(), this);
        mLocationTv = (TextView) findViewById(R.id.start_live_tv_location);
        mOtherView = findViewById(R.id.start_live_ll_other);
        mStartBtn = (Button) findViewById(R.id.start_live_btn_start);

        initClickListener();
    }

    protected abstract int getLayoutId();

    @CallSuper
    protected void initClickListener() {
        findViewById(R.id.start_live_iv_close).setOnClickListener(this);
        findViewById(R.id.start_live_tv_location).setOnClickListener(this);
        findViewById(R.id.start_live_iv_switch).setOnClickListener(this);
        findViewById(R.id.start_live_iv_beauty).setOnClickListener(this);
        findViewById(R.id.start_live_btn_start).setOnClickListener(this);
        findViewById(R.id.start_live_iv_bgm).setOnClickListener(this);
    }

    public void setOnActionClickListener(OnActionClickListener listener) {
        mListener = listener;
    }

    public void setLocationStatus(boolean open) {
        mLocationTv.setText(open ? R.string.push_location_on : R.string.push_location_off);
    }

    public void onShowBeautyView() {
        setBackgroundColor(Color.TRANSPARENT);
        if (mOtherView != null) {
            ViewUtil.startVisibilityAnimation(mOtherView, GONE);
        }
    }

    public void onHideBeautyView() {
        setBackgroundColor(getResources().getColor(R.color.bg_7f00));
        if (mOtherView != null) {
            ViewUtil.startVisibilityAnimation(mOtherView, VISIBLE);
        }
    }

    protected void setBGMViewVisible(boolean visible){
        findViewById(R.id.start_live_iv_bgm).setVisibility(visible?View.VISIBLE:View.GONE);
    }

    @CallSuper
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start_live_iv_close:
                mListener.onCloseClick();
                break;
            case R.id.start_live_tv_location:
                mListener.onLocationClick();
                break;
            case R.id.start_live_iv_beauty:
                mListener.onBeautyClick();
                break;
            case R.id.start_live_iv_switch:
                mListener.onCameraClick();
                break;
            case R.id.start_live_btn_start:
                MobclickAgent.onEvent(getContext(), "cdb_challenge_open");
                MobclickAgent.onEvent(getContext(), "cdb_ready_start");
                mListener.onStartClick();
                break;
            case R.id.start_live_iv_bgm:
                mListener.onBGMClick();
                break;
        }
    }

    public  interface OnActionClickListener {
        void onLocationClick();
        void onCameraClick();
        void onBeautyClick();
        void onCloseClick();
        void onStartClick();
        void onBGMClick();
    }

}
